package com.gluck.cases.python;

import com.gluck.cases.BaseTest;
import org.junit.Test;

import java.io.BufferedReader;
import java.io.InputStreamReader;

/**
 * @ClassName ExcutePy
 * @Desc TODO
 * @Author shiyongzhe
 * @Date 2022/5/27 - 10:19 下午
 **/
public class ExcutePy extends BaseTest {

    @Test
    public void testExcutePy()throws Exception{
//        String exe = "python";
//        String command = "/Users/shiyongzhe/myworkspace/javacode/gluck-stock/stockmanagesystem/oauth-system8881/test/src/test/java/com/gluck/stock/oauth/python/main.py";
//        String command = "/Users/shiyongzhe/myworkspace/pythoncode/gitee/cases/main.py";
//
//        String[] cmdArr = new String[] {exe, command};
//        Process process = Runtime.getRuntime().exec(cmdArr);
//        InputStream is = process.getInputStream();
//        DataInputStream dis = new DataInputStream(is);
//        String str = dis.readLine();
//        process.waitFor();
//        System.out.println(str);
//        String pyPath = "/Users/shiyongzhe/myworkspace/pythoncode/gitee/cases/main.py"; // python文件路径
//        String pyEnvironment = "python"; // 默认为python，如果使用了anaconda创建了环境，可以找到对应的路径并替换，类似于"E:\\Anaconda3\\envs\\xxx\\python.exe"。
//        PythonRun pythonRun = new PythonRun(); // 创建实例
//        pythonRun.setEnvironment(pyEnvironment); // 设置环境
//        pythonRun.setRoot("/Users/shiyongzhe/myworkspace/pythoncode/gitee/cases/"); // 设置python项目的执行目录，若不设置，在调用了其它包时，可能会出现错误。如果没有import其它文件夹下的包或库，可以忽略。
//        System.out.println(pythonRun.run(pyPath));
            String arguments = "/Users/shiyongzhe/myworkspace/javacode/gluck-stock/stockmanagesystem/oauth-system8881/test/src/test/java/com/gluck/stock/oauth/python/main.py";


            ProcessBuilder processBuilder = new ProcessBuilder(arguments);
            StringBuilder stringBuilder = new StringBuilder();
            processBuilder.redirectErrorStream(true);
            Process process =null;
            try {
                // 获取程序执行后返回的结果
                //执行这个.exe程序
                process = processBuilder.start();
//            process = Runtime.getRuntime().exec("cmd /c start " + arguments);
                BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream(),"GBK"));
                String line = null;
                while ((line = in.readLine()) != null) {
//                System.out.println(line);
                    stringBuilder.append(line + System.lineSeparator());
                }
                in.close();
                //java代码中的process.waitFor()返回值为0表示我们执行.exe文件成功，
                //返回值为1表示执行.exe文件失败，这和我们通常意义上见到的0与1定义正好相反
                int re = process.waitFor();
                System.out.println(re);
                System.out.println(stringBuilder.toString().trim()+"-------------");
            } catch (Exception e) {
                e.printStackTrace();
            }finally{
                if (process != null) {
                    process.destroy();
                }
            }
    }
}
